#ifndef THREAD_H_
#define THREAD_H_

#include <windows.h>
#include <process.h>

class Thread
{
    //member data
protected:
    bool m_tActive;
    HANDLE m_tHandle;
    unsigned int m_tID;
    //Creation
public:
    Thread() :    m_tActive(true), 
        m_tHandle(0),
        m_tID(0)
    {
        m_tHandle = (HANDLE)_beginthreadex(   NULL,       //security attribute
            0,                                  //default stack size
            ThreadStaticStartUp,                //this functions calls Run
            this,                               //the data passed to StartUp
            CREATE_SUSPENDED,                   //start in a suspended state
            &m_tID);                            //where to store the ID
    }
    virtual ~Thread() = 0 {CloseHandle(m_tHandle);}

    //usage
public:
    //Start the thread!
    void Start()
    {
        ResumeThread(m_tHandle);
    }
    //this must return 1 before it is safe to be deleted
    unsigned long GetExitCode()
    {
        unsigned long returnValue = 0;
        GetExitCodeThread(m_tHandle, &returnValue);
        return returnValue;
    }
    //usefull
    virtual int WaitForFinish()
    {
        WaitForSingleObject(m_tHandle, INFINITE);
        return GetExitCode();
    }
    //If this type of thread supports stopping early,pausing or resuming
    virtual void vStop() = 0;
    virtual void vPause() = 0;
    virtual void vResume() = 0;
    //This could be different depending on the type of thread, 
    virtual bool vHasFinished()
    {
        return !m_tActive;
    }
    //static run function
protected:
    static unsigned __stdcall ThreadStaticStartUp(void* pThis)
    {
        Thread* pthY = (Thread*)pThis;
        pthY->vRun();
        return 1;
    }

    //these need to be definged...Run by the type of thread, and Process by the game 
    //specific function
    virtual void vRun() = 0;
    virtual void vProcess() = 0;
};

#endif //THREAD_H_